package com.kotlin.main

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.view.Window
import android.view.WindowManager
import com.kotlin.base.http.YHttp
import com.kotlin.base.utils.JsonUtil
import com.kotlin.main.R.id.*
import com.kotlin.main.bean.CelebratedDictum
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Function
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_launch.*
import okhttp3.ResponseBody
import java.text.SimpleDateFormat
import java.util.*

/**
 * 类 名: LaunchActivity
 * 作 者: yzhg
 * 创 建: 2018/11/16 0016
 * 版 本: 1.0
 * 历 史: (版本) 作者 时间 注释
 * 描 述: 启动页面
 */
class LaunchActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        this.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        setContentView(R.layout.activity_launch)
        alpha()
        launchStart.setOnClickListener { MainActivity.startIntent(this) }
    }


    //2.做ImageView动画
    private fun alpha() {
        val alpha: ObjectAnimator = ObjectAnimator.ofFloat(iv_launch, "alpha", 0f, 1f).setDuration(800)
        alpha.start()
        alpha.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                tv_count_down.setText("倒计时${getGapCount()}天")
                getCelebratedDictum() //获取网络数据
            }
        })
    }


    /**
     * 计算时间
     */
    fun getGapCount(): Long {
        val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)//yyyy-mm-dd, 会出现时间不对, 因为小写的mm是代表: 秒
        sdf.timeZone = TimeZone.getTimeZone("GMT+8")
        val startParse: Date = sdf.parse(sdf.format(Date(System.currentTimeMillis())))
        val endParse: Date = sdf.parse("2019-04-12")

        val calendar: Calendar = Calendar.getInstance()
        calendar.setTime(startParse)
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)

        val toCalendar: Calendar = Calendar.getInstance()
        toCalendar.setTime(endParse)
        toCalendar.set(Calendar.HOUR_OF_DAY, 0)
        toCalendar.set(Calendar.MINUTE, 0)
        toCalendar.set(Calendar.SECOND, 0)
        toCalendar.set(Calendar.MILLISECOND, 0)
        return (toCalendar.time.time - calendar.time.time) / (1000 * 60 * 60 * 24)
    }

    fun getCelebratedDictum() {
        val map: MutableMap<String, String> = mutableMapOf()
        map.put("key", "2683042dfe4b403db78ff3d66f6f4ef1")
        val url = "MingRenMingYan/Random"
        val builder: YHttp = YHttp.Builder().setBaseUrl("http://api.avatardata.cn/").build()
        builder.requestPostRx(url, map)
                .subscribeOn(Schedulers.io())
                .map(object : Function<ResponseBody, CelebratedDictum> {
                    override fun apply(responseBody: ResponseBody): CelebratedDictum {
                        val json: String = responseBody.string()
                        return JsonUtil.get().parseJsonToBean(json, CelebratedDictum::class.java)!!
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<CelebratedDictum> {
                    override fun onComplete() {

                    }

                    override fun onSubscribe(d: Disposable) {

                    }

                    override fun onNext(celebratedDictum: CelebratedDictum) {
                        if ("Succes" == celebratedDictum.reason) {
                            tv_famous_saying.text = celebratedDictum.result.famous_saying
                            tv_count_down.text = celebratedDictum.result.famous_name
                        }
                    }

                    override fun onError(e: Throwable) {

                    }

                })
    }
}














